<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        const url = 'http://www.liulongbin.top:3006/api/news';
        const params = {
            name: 'zs',
            age: 18,
        }
        const res = [];
        for (const key in params) {
            res.push([key,params[key]].join('='));
        }
        const str = `?${res.join('&')}`;
        console.log(str);
        // 1.创建实例
        const xhr = new XMLHttpRequest();
        // 2.填写接口信息
        xhr.open('get', url + str);
        // 3.发送
        xhr.send();
        // 4.接返回值 监听onreadystatechange事件
        xhr.onreadystatechange = function () {
            // 4.1 监听 xhr 对象的请求状态 readyState;与服务器响应的状态 status
            if (xhr.readyState === 4 && xhr.status === 200) {
                // 4.2 打印服务器响应回来的数据
                console.log(JSON.parse(xhr.responseText));
            }
        }
    </script>
</body>
</html>